﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DIS.Infrastructure.Entities.Entity;
using DIS.Tool.Framework;
using System.Web.Security;
using System.Web;
using DIS.Infrastructure.Entities;
using System.Data.SqlClient;
using System.Data;

namespace DIS.Portal.Biz
{
    public class CityTaxiFeeBiz
    {
        public static CityTaxi SaveCityTaxiFee(CityTaxi citytaxi, List<CityTaxiDetail> detailList)
        {
            if (citytaxi.Id > 0)
            {
               var citytaxiTemp = new CityTaxi().Find(citytaxi.Id.ToString());
               citytaxiTemp.Year = citytaxi.Year;
               citytaxiTemp.Month = citytaxi.Month;
               citytaxiTemp.StartDate = citytaxi.StartDate;
               citytaxiTemp.EndDate = citytaxi.EndDate;
               citytaxiTemp.TaxiFee = citytaxi.TaxiFee;
               citytaxiTemp.InvoiceCount = citytaxi.InvoiceCount;
               citytaxiTemp.ProjectId = citytaxi.ProjectId;
               citytaxiTemp.PostModify();
            }
            else
            {
                citytaxi = citytaxi.PostAdd();
            }
            foreach (var item in detailList)
            {
                item.CityTaxiId = citytaxi.Id;
                item.PostAdd();
            }
            return citytaxi;
        }

        public static ItemListBox<CityTaxi> GetAllCityTaxiFee(int page = 0,int pageSize=20)
        {
            string username=System.Web.HttpContext.Current.User.Identity.Name;
            var count = new CityTaxi().Count(c=>c.UserName==username);
            using (Context ctx = new Context())
            {
                var data = (from c in ctx.CityTaxi
                            join d in ctx.User
                            on c.UserName equals d.UserName
                            join f in ctx.Project
                            on c.ProjectId equals f.Id
                            where c.UserName == username
                            orderby c.Year, c.Month descending
                            select new { c, d,f })
                          .ToList()
                          .Select(e => new CityTaxi().Set(e.c).LoadUser(e.d).LoadProject(e.f)).ToList();

                return new ItemListBox<CityTaxi>(data).BuildPage(
                  count, page, 20, new PageParameter() { Style = "workspace" });
            }
        }
    }
}
